﻿/************************************************************************************
 * xujiajun@yoozoo.com 御剑
 * 供Lua端在运行时动态添加的支持LOD的层级
 ***********************************************************************************/

using XLua;
using Unity.Mathematics;
using WorldMapBase.LOD;

namespace WorldMapBase
{
    /// <summary>
    /// 供Lua端在运行时动态添加的支持LOD的层级
    /// </summary>
    public class LuaLODLayer : WorldMapBase.Layer.Layer, ILODListener
    {
        /// <summary>
        /// C#触发LOD等级变化时调用Lua的方法名。传参为(int currentLodLevel, int lastLodLevel)
        /// </summary>
        public static string FunctionName_OnLODLevelChange = "OnLODLevelChange";

        /// <summary>
        /// C#触发LOD百分比变化时调用Lua的方法名。传参为(int lodLevel, int nextLevelPercent)
        /// </summary>
        public static string FunctionName_OnNextLODPercentUpdate = "OnNextLODPercentUpdate";

        /// <summary>
        /// C#触发层级创建时调用Lua的方法名
        /// </summary>
        public static string FunctionName_OnCreate = "OnCreate";

        /// <summary>
        /// C#触发层级销毁时调用Lua的方法名
        /// </summary>
        public static string FunctionName_OnDestroy = "OnDestroy";

        /// <summary>
        /// C#触发镜头视野变化更新调用Lua的方法名。传参为(int x, int y, int width, int height)
        /// </summary>
        public static string FunctionName_OnUpdateViewRect = "OnUpdateViewRect";

        private LuaTable m_LuaTable;
        private LuaFunction m_LuaFunction_OnLODLevelChange;
        private LuaFunction m_LuaFunction_OnNextLODPercentUpdate;
        private LuaFunction m_LuaFunction_OnCreate;
        private LuaFunction m_LuaFunction_OnDestroy;
        private LuaFunction m_LuaFunction_OnUpdateViewRect;

        public LuaLODLayer(LuaTable luaTable)
        {
            m_LuaTable = luaTable;
            m_LuaFunction_OnLODLevelChange = m_LuaTable.Get<LuaFunction>(FunctionName_OnLODLevelChange);
            m_LuaFunction_OnNextLODPercentUpdate = m_LuaTable.Get<LuaFunction>(FunctionName_OnNextLODPercentUpdate);
            m_LuaFunction_OnCreate = m_LuaTable.Get<LuaFunction>(FunctionName_OnCreate);
            m_LuaFunction_OnDestroy = m_LuaTable.Get<LuaFunction>(FunctionName_OnDestroy);
            m_LuaFunction_OnUpdateViewRect = m_LuaTable.Get<LuaFunction>(FunctionName_OnUpdateViewRect);
        }

        public void OnLODLevelChange(int currentLodLevel, int lastLodLevel)
        {
            m_LuaFunction_OnLODLevelChange?.Call(currentLodLevel, lastLodLevel);
        }

        public void OnNextLODPercentUpdate(int lodLevel, float nextLevelPercent)
        {
            m_LuaFunction_OnNextLODPercentUpdate?.Call(lodLevel, nextLevelPercent);
        }

        protected override void OnCreate()
        {
            m_LuaFunction_OnCreate?.Call();
        }

        protected override void OnDestroy()
        {
            m_LuaFunction_OnDestroy?.Call();
        }

        protected override void OnUpdateViewRect(int4 viewRect)
        {
            m_LuaFunction_OnUpdateViewRect?.Call(viewRect.x, viewRect.y, viewRect.z, viewRect.w);
        }
    }

}


